﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KpiConfigProject.Manager;

namespace KpiConfigProject.TypeEditor.Controls
{
    public partial class FormulaEditorCtrl : Form
    {
        public string FormulaString
        {
            get { return rtbe_Formula.Text; }
            set { rtbe_Formula.Text = value; }
        }

        public FormulaEditorCtrl()
        {
            InitializeComponent();
        }

        private Dictionary<string, DataRow> counterDic, kpiFlagDic, msgFalgDic;
        private void FormulaEditorCtrl_Load(object sender, EventArgs e)
        {
            InitAllData();
        }

        private void InitAllData()
        {
            counterDic = new Dictionary<string, DataRow>();
            kpiFlagDic = new Dictionary<string, DataRow>();
            msgFalgDic = new Dictionary<string, DataRow>();
            //CounterManager
            CounterManager counterManager = new CounterManager();
            DataTable table = counterManager.GetList();
            if (table != null)
            {
                dgve_Counter.DataSource = table.DefaultView;

                foreach (DataRow row in table.Rows)
                {
                    counterDic.Add(row["vcAliasName"].ToString(), row);
                }
            }

            KpiFlagManager kpiFlagManager = new KpiFlagManager();
            table = kpiFlagManager.GetList();
            if (table != null)
            {
                dgve_KpiFlag.DataSource = table.DefaultView;
                foreach (DataRow row in table.Rows)
                {
                    kpiFlagDic.Add(row["vcFlagName"].ToString(), row);
                }
            }

            MsgFlagManager msgFlagManager = new MsgFlagManager();
            table = msgFlagManager.GetList();
            if (table != null)
            {
                dgve_MsgFlag.DataSource = table.DefaultView;
                foreach (DataRow row in table.Rows)
                {
                    msgFalgDic.Add(row["vcFlagName"].ToString(), row);
                }
            }

            KpiInfoManager kpiInfoManager = new KpiInfoManager();
            table = kpiInfoManager.GetList();
            if (table != null)
                dgve_KpiInfo.DataSource = table.DefaultView;
        }

        private void dgve_Counter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.C)
            {
                string selectText;
                int rowIndex;
                switch (tabControl1.SelectedIndex)
                {
                    case 0:
                        if (dgve_Counter.SelectedCells.Count <= 0)
                            return;
                        rowIndex = dgve_Counter.SelectedCells[0].RowIndex;
                        selectText = "{C:" + dgve_Counter.Rows[rowIndex].Cells["vcAliasName"].Value.ToString() + "}";
                        Clipboard.SetText(selectText);
                        return;
                    case 1:
                        if (dgve_KpiFlag.SelectedCells.Count <= 0)
                            return;
                        rowIndex = dgve_KpiFlag.SelectedCells[0].RowIndex;
                        selectText = "{KF:" + dgve_KpiFlag.Rows[rowIndex].Cells["vcFlagName"].Value.ToString() + "}";
                        Clipboard.SetText(selectText);
                        return;
                    case 2:
                        if (dgve_MsgFlag.SelectedCells.Count <= 0)
                            return;
                        rowIndex = dgve_MsgFlag.SelectedCells[0].RowIndex;
                        selectText = "{MF:" + dgve_MsgFlag.Rows[rowIndex].Cells["vcFlagName"].Value.ToString() + "}";
                        Clipboard.SetText(selectText);
                        return;
                    case 3:
                        if (dgve_KpiInfo.SelectedCells.Count <= 0)
                            return;
                        rowIndex = dgve_KpiInfo.SelectedCells[0].RowIndex;
                        selectText = "{K:" + dgve_KpiInfo.Rows[rowIndex].Cells["vcAliasName"].Value.ToString() + "}";
                        Clipboard.SetText(selectText);
                        return;
                }
            }
        }

        private void rtb_FormulaPreview_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        private void ReflashPreviewFormula()
        {
            string formula = rtbe_Formula.Text;

        }
    }
}
